package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.sym.NameN;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //设置分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        page = setmealMapper.selectPage(page, wrapper);
        //遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                //根据catrgoryid去catrgory查询
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                //4.根据套餐id去setmealdish表查询
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                //select * from setmeal_dish where setmeal_id =#{}
                wrapper1.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
                return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //1.基本信息保存到套餐里
        setmealMapper.insert(setmeal);
        //2.遍历setmealdish得到多个setmealdish
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                //设置套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //将套餐保存到stemeal_dish表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void deleteByIds(Long[] ids) {
        //delete count(0) from sermeal where id in #{ids} and status=1
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);//在售
        Integer count = setmealMapper.selectCount(wrapper);
        if (count>0){
            throw new CustomException("该套餐为在售状态，不允许删除");
        }
        //删除setmeal_dish
        //delete from setmeal_dish where setmeal_id in #{ids}
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper1);
        //删除套餐表
        //delete from setmeal where status = 0 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(Setmeal::getId,ids)
                .eq(Setmeal::getStatus, 0);//不在售
        setmealMapper.delete(wrapper2);
    }

    @Override
    public void findByIds(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }

    }

    @Override
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;

    }

    @Override
    public void update(Setmeal setmeal) {
        //根据id更新基本信息
        setmealMapper.selectById(setmeal);
        //删除套餐表中基本信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getDishId, setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //将修改的菜品重新保存到套餐分类
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
           /* LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.like(Dish::getName, setmeal.getName());
            setmealDishMapper.selectList(wrapper1);*/
        }
    }

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }

    @Override
    public void findByName(Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, ids)
                .eq(Setmeal::getStatus, 1);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(setmealList)){
            for (Setmeal setmeal : setmealList) {
                LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.like(Dish::getName, setmeal.getName());
                List<Dish> dishList = dishMapper.selectList(wrapper1);
            }
        }
    }

    @Override
    public Page<Setmeal> findByName(String name, Integer pageNum, Integer pageSize) {
        //设置分页
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        page=setmealMapper.selectPage(page, wrapper);
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.like(SetmealDish::getName, name);
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        return page;
    }

    @Override
    public List<Dish> getMealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            //设置份数
            dish.setCopies(setmealDish.getCopies());
            dishList.add(dish);
        }
        return dishList;
    }

}